package com.lyf.controll;

import cn.hutool.json.JSONObject;
import com.lyf.pojo.Dept;
import com.lyf.pojo.Employee;
import com.lyf.result.Code;
import com.lyf.result.Result;
import com.lyf.service.EmployeeService;
import com.lyf.service.impl.EmployeeServiceImpl;
import com.lyf.utils.PageModel;
import com.lyf.utils.ResponseResult;
import com.lyf.vo.CountVoByName;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "EmployeeControll",urlPatterns = {"/employee/addOrupdate","/employee/delete","/employee/updateStatus",
        "/employee/selectCountByDeptId","/employee/selectCountByJobId","/employee/selectCountBySpeciality","/employee/selectCountByEducation",
        "/employee/selectCountByRace","/employee/selectCountByProvince","/employee/selectCountByParty","/employee/selectById",
        "/employee/selectByname","/employee/queryEmployee"
})
public class EmployeeControll extends HttpServlet {
    EmployeeService employeeService = new EmployeeServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String servletPath = req.getServletPath();
        System.out.println("请求路径："+servletPath);
        if(servletPath.equals("/employee/addOrupdate")){
            String id = req.getParameter("id");
            if(id != null) {
                //修改
                String dept_id = req.getParameter("dept_id");
                String job_id = req.getParameter("job_id");
                String name = req.getParameter("name");
                String card_id = req.getParameter("card_id");
                String address = req.getParameter("address");
                String province = req.getParameter("province");
                String post_code = req.getParameter("postCode");
                String phone = req.getParameter("phone");
                String qqNum = req.getParameter("qqNum");
                String email = req.getParameter("email");
                String sex = req.getParameter("sex");
                String party = req.getParameter("party");
                String birthday = req.getParameter("birthday");
                String birthday2 = req.getParameter("birthday2");
                String race = req.getParameter("race");
                String education = req.getParameter("education");
                String speciality = req.getParameter("speciality");
                String hobby = req.getParameter("hobby");
                String remark = req.getParameter("remark");
                String status = req.getParameter("status");

                Employee employee = new Employee();
                employee.setDept_id(Integer.parseInt(dept_id));
                employee.setJob_id(Integer.parseInt(job_id));
                employee.setName(name);
                employee.setCard_id(card_id);
                employee.setAddress(address);
                employee.setProvince(province);
                employee.setPostCode(post_code);
                employee.setPhone(phone);
                employee.setQqNum(qqNum);
                employee.setEmail(email);
                employee.setSex(Integer.parseInt(sex));
                employee.setParty(party);
                if(birthday2 != null && !"".equals(birthday2)){
                    // 定义日期时间格式
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                    // 解析字符串为LocalDateTime
                    LocalDateTime birthdayTime = LocalDateTime.parse(birthday2.trim(), formatter);
                    employee.setBirthday(birthdayTime);
                }else{
                    long timestamp = Long.parseLong(birthday); // 将时间戳转换为长整型
                    // 将时间戳转换为 LocalDateTime
                    LocalDateTime birthdayTime = Instant.ofEpochMilli(timestamp)
                            .atZone(ZoneId.systemDefault())
                            .toLocalDateTime();
                    employee.setBirthday(birthdayTime);
                }
                employee.setRace(race);
                employee.setEducation(education);
                employee.setSpeciality(speciality);
                employee.setHobby(hobby);
                employee.setRemark(remark);
                employee.setStatus(Integer.parseInt(status));
                employee.setId(Integer.parseInt(id));

                employeeService.update(employee);

                Result result = new Result(Code.OK,null,"修改成功！");
                ResponseResult.sendResult(resp,result);
            }else{
                //增加
                String dept_id = req.getParameter("dept_id");
                String job_id = req.getParameter("job_id");
                String name = req.getParameter("name");
                String card_id = req.getParameter("card_id");
                String address = req.getParameter("address");
                String province = req.getParameter("province");
                String post_code = req.getParameter("postCode");
                String phone = req.getParameter("phone");
                String qqNum = req.getParameter("qqNum");
                String email = req.getParameter("email");
                String sex = req.getParameter("sex");
                String party = req.getParameter("party");
                String birthday = req.getParameter("birthday2").trim();
                String race = req.getParameter("race");
                String education = req.getParameter("education");
                String speciality = req.getParameter("speciality");
                String hobby = req.getParameter("hobby");
                String remark = req.getParameter("remark");

                Employee employee = new Employee();
                employee.setDept_id(Integer.parseInt(dept_id));
                employee.setJob_id(Integer.parseInt(job_id));
                employee.setName(name);
                employee.setCard_id(card_id);
                employee.setAddress(address);
                employee.setProvince(province);
                employee.setPostCode(post_code);
                employee.setPhone(phone);
                employee.setQqNum(qqNum);
                employee.setEmail(email);
                employee.setSex(Integer.parseInt(sex));
                employee.setParty(party);
                // 定义日期时间格式
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                // 解析字符串为LocalDateTime
                LocalDateTime birthdayTime = LocalDateTime.parse(birthday, formatter);
                employee.setBirthday(birthdayTime);
                employee.setRace(race);
                employee.setEducation(education);
                employee.setSpeciality(speciality);
                employee.setHobby(hobby);
                employee.setRemark(remark);
                // 获取当前时间
                LocalDateTime createdate = LocalDateTime.now();
                employee.setCreateDate(createdate);

                employeeService.add(employee);

                Result result = new Result(Code.OK,null,"添加成功！");
                ResponseResult.sendResult(resp,result);
            }
        }else if(servletPath.equals("/employee/delete")){
            String id = req.getParameter("id");

            employeeService.delete(Integer.parseInt(id));

            Result result = new Result(Code.OK,null,"删除成功！");
            ResponseResult.sendResult(resp,result);
        }else if(servletPath.equals("/employee/updateStatus")){
            String id = req.getParameter("id");
            String status = req.getParameter("status");

            employeeService.updateStatus(Integer.parseInt(id),Integer.parseInt(status));

            Result result = new Result(Code.OK,null,"状态修改成功！");
            ResponseResult.sendResult(resp,result);
        }else if(servletPath.equals("/employee/selectCountByDeptId")){
            List<CountVoByName> countVoByNames = employeeService.selectCountByDeptId();
            Result result = new Result(Code.OK,countVoByNames,"查询成功！");
            ResponseResult.sendResult(resp,result);
        }else if(servletPath.equals("/employee/selectCountByJobId")){
            List<CountVoByName> countVoByNames = employeeService.selectCountByJobId();
            Result result = new Result(Code.OK,countVoByNames,"查询成功！");
            ResponseResult.sendResult(resp,result);
        }else if(servletPath.equals("/employee/selectCountBySpeciality")){
            List<CountVoByName> countVoByNames = employeeService.selectCountBySpeciality();
            Result result = new Result(Code.OK,countVoByNames,"查询成功！");
            ResponseResult.sendResult(resp,result);
        }else if(servletPath.equals("/employee/selectCountByEducation")){
            List<CountVoByName> countVoByNames = employeeService.selectCountByEducation();
            Result result = new Result(Code.OK,countVoByNames,"查询成功！");
            ResponseResult.sendResult(resp,result);
        }else if(servletPath.equals("/employee/selectCountByRace")){
            List<CountVoByName> countVoByNames = employeeService.selectCountByRace();
            Result result = new Result(Code.OK,countVoByNames,"查询成功！");
            ResponseResult.sendResult(resp,result);
        }else if(servletPath.equals("/employee/selectCountByProvince")){
            List<CountVoByName> countVoByNames = employeeService.selectCountByProvince();
            Result result = new Result(Code.OK,countVoByNames,"查询成功！");
            ResponseResult.sendResult(resp,result);
        }else if(servletPath.equals("/employee/selectCountByParty")){
            List<CountVoByName> countVoByNames = employeeService.selectCountByParty();
            Result result = new Result(Code.OK,countVoByNames,"查询成功！");
            ResponseResult.sendResult(resp,result);
        }
        else if(servletPath.equals("/employee/selectById")){
            String id = req.getParameter("id");
            Employee employee = employeeService.selectById(Integer.parseInt(id));

//            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//            String formattedBirthday = employee.getBirthday().format(formatter);

            Result result = new Result(Code.OK,employee,"查询成功！");
            ResponseResult.sendResult(resp,result);
        }else if(servletPath.equals("/employee/selectByname")){
            String name = req.getParameter("name");
            Employee employee = employeeService.selectByname(name);

            Result result = new Result(Code.OK,employee,"查询成功！");
            ResponseResult.sendResult(resp,result);
        }else if(servletPath.equals("/employee/queryEmployee")){
            //获得当前页码
            String pageIndex = req.getParameter("pageNum");
            //获取每页显示条数
            String pageSize = req.getParameter("pageSize");

            //当前页码
            int pageNum = Integer.parseInt(pageIndex);
            //每页显示条数
            int pageSize2 = Integer.parseInt(pageSize);

            //设置到PageModel
            PageModel model = new PageModel();
            model.setPageIndex(pageNum);
            model.setPageSize(pageSize2);

            //获取查询条件
            String dept_id = req.getParameter("dept_id");
            String job_id = req.getParameter("job_id");
            String name = req.getParameter("name");
            String card_id = req.getParameter("card_id");
            String phone = req.getParameter("phone");
            String sex = req.getParameter("sex");
            String status = req.getParameter("status");


            //查询用户列表
            List<Employee> employeeList = null;

            Employee employee = new Employee();
            if(!dept_id.equals("") && !dept_id.isEmpty()){
                employee.setDept_id(Integer.parseInt(dept_id));
            }
            if(!job_id.equals("") && !job_id.isEmpty()){
                employee.setJob_id(Integer.parseInt(job_id));
            }
            if(!name.equals("") && !name.isEmpty()){
                employee.setName(name);
            }
            if(!card_id.equals("") && !card_id.isEmpty()){
                employee.setCard_id(card_id);
            }
            if(!phone.equals("") && !phone.isEmpty()){
                employee.setPhone(phone);
            }
            if(!sex.equals("") && !sex.isEmpty()){
                employee.setSex(Integer.parseInt(sex));
            }
            if(!status.equals("") && !status.isEmpty()){
                employee.setStatus(Integer.parseInt(status));
            }
            //总记录数
            int count = employeeService.getTotalEmployeeCount(employee);
            model.setTotalRecoreSum(count);
            employeeList = employeeService.queryEmployee(employee,model);

            Map<String,Object> map = new HashMap<>();
            map.put("model",model);
            System.out.println("model:"+model);
            map.put("data",employeeList);

            JSONObject jo = new JSONObject();
            jo.putAll(map);
            resp.getWriter().println(jo.toString());//响应到浏览器
        }
    }
}
